//
//  ChannelViewController.m
//  Movie
//
//  Created by cjy on 16/3/25.
//  Copyright © 2016年 HY. All rights reserved.
//

#import "ChannelViewController.h"
#import "MHNetWorkTask.h"
#import "ChannelCollectionViewCell.h"
#import "ChannelModel.h"
#import "PushChannel.h"

@interface ChannelViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,retain)UICollectionView *collectionView;
@property (nonatomic,retain)UICollectionViewFlowLayout *flowlayout;
@property (nonatomic,retain)NSMutableArray *array;


@end

@implementation ChannelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"频道";
    

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target: self action:@selector(presentLeftMenuViewController:)];
    

    [self handle];
    
    [self createCollectionView];
}



- (void)handle{
    
    self.array = [NSMutableArray array];
       NSMutableDictionary *dic =@{@"App-Id" : @"10101027",
                                @"Device-Id" : @"616b51f3e3729568db23640554991556",
                                @"Device-V" : @"aVBob25lIE9TXzkuMi4xXzY0MCoxMTM2XzEwMDAwMTAwMF9pUGhvbmUgNVM="}.mutableCopy;
    
    [MHNetWorkTask getWithURL:@"http://mapi.yinyuetai.com/recommend/video/aggregation.json" withParameter:nil withHttpHeader:dic withResponseType:ResponseTypeJSON withSuccess:^(id result) {
       
        
        NSArray *arr = [result objectForKey:@"data"];
        
        for (NSDictionary *dicdata in arr) {
            
        
        ChannelModel *model = [[ChannelModel alloc]init];
            
            model.img = [dicdata objectForKey:@"img"];
            model.title = [dicdata objectForKey:@"title"];
            model.ida = [dicdata objectForKey:@"id"];
            [self.array addObject:model];
        }
        
        [self.collectionView reloadData];
        
        
    } withFail:^(NSError *error) {
        
        
    }];
    
}



- (void)createCollectionView{

    self.flowlayout = [[UICollectionViewFlowLayout alloc]init];
    
//    self.flowlayout.itemSize = CGSizeMake((self.view.frame.size.width - 10)/2*1.6, (self.view.frame.size.width - 10)/2*1.6);
//    
//    self.flowlayout.minimumLineSpacing = 10;
//    self.flowlayout.minimumInteritemSpacing = 0;
    
    self.flowlayout = [[UICollectionViewFlowLayout alloc]init];
    self.flowlayout.itemSize = CGSizeMake((self.view.frame.size.width - 30) / 2.0, ((self.view.frame.size.width - 30) / 2.0));

    self.flowlayout.sectionInset = UIEdgeInsetsMake(10,10, 10, 10);

    self.collectionView = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:self.flowlayout];
    
    [self.view addSubview:self.collectionView];

    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.collectionView registerClass:[ChannelCollectionViewCell class] forCellWithReuseIdentifier:@"pool"];
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.array.count;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    ChannelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    
    ChannelModel *model = self.array[indexPath.item];
    [cell passModel:model];
    
    return cell;
}



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    PushChannel *push = [[PushChannel alloc]init];
    
    ChannelModel *model = self.array[indexPath.item];
    
    push.ida = [model.ida integerValue];
    push.title2 = model.title;

    [self.navigationController pushViewController:push animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
